package com.jiajia.study.chapter03.method;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author zjiajia
 * @date 2023/11/2 9:27
 */
@Slf4j(topic = "c.method")
public class TestMethod {
    public static Integer number = 0;
    public static void main(String[] args) throws InterruptedException {
//        testStartAndRun();
//        testSleep();
//        testYield();
//        testJoin();
//        testInterrupt();
//        testInterruptNormal();
//        testTwoPhaseTermination();
//        testParkInterrupt();
//        testParkFail();
        testDaemon();
    }

    public static void testDaemon() throws InterruptedException {
        Thread thread = new Thread(()->{
            log.debug("开始执行。。。。。");
            try {
                TimeUnit.SECONDS.sleep(10L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("结束执行");
        },"daemon");
        thread.setDaemon(true);

        thread.start();

        TimeUnit.SECONDS.sleep(1);

        log.debug("结束执行>>>>>>>>>>>>>>>>>>>>>>");
    }

    public static void testParkFail() throws InterruptedException {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 5; i++) {
                log.debug("park..........");
                LockSupport.park();
                log.debug("unpark..........");
                log.debug("this thread interrupt state is : " + Thread.currentThread().isInterrupted());
            }
        },"t1");
        thread.start();

        Thread.sleep(1000L);

        thread.interrupt();
    }

    public static void testParkInterrupt() throws InterruptedException {
        Thread thread = new Thread(()->{
            log.debug("park..........");
            LockSupport.park();
            log.debug("unpark..........");
            log.debug("this thread interrupt state is : " + Thread.currentThread().isInterrupted());
        },"t1");
        thread.start();

        Thread.sleep(1000L);

        thread.interrupt();

    }
    public static void testTwoPhaseTermination() throws InterruptedException {

        Monitor monitor = new Monitor();
        // 开启监控
        monitor.start();
        // 停止
        Thread.sleep(10000L);
        monitor.stop();
    }

    public static void testInterruptNormal(){
        Thread thread = new Thread(()->{

            while(true){

            }
        },"t1");

        thread.start();
        log.debug("thread interrupt is : {}",thread.isInterrupted());
        thread.interrupt();
        log.debug("thread interrupt is : {}",thread.isInterrupted());

    }

    public static void testStartAndRun(){

        new Thread(()->{
            log.debug("执行了.................");
        },"t1").start();

        new Thread(()->{
            log.debug("执行了.................");
        },"t2").run();

        log.debug("main 方法执行完成.................");
    }

    public static void testSleep(){
        new Thread(()->{
            try {
                Thread.sleep(10000);
                log.debug("执行完成.....................");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();

        log.debug("执行完成");
    }

    public static void testYield(){
        new Thread(()->{
            int count = 0;
            while(true){
                Thread.yield();
                log.debug(" the count is : {}",++count);
            }

        },"t1").start();
        new Thread(()->{
            int count = 0;
            while(true){
                log.debug(">>>>>>>>>>>>>>>>>>>>>>> the count is : {}",++count);
            }

        },"t2").start();

    }

    public static void testJoin() throws InterruptedException {

        Thread thread = new Thread(() -> {
            number++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        thread.start();

        log.debug("the number is : {}",number);
        // 等待 t1 执行完成
        thread.join();
        log.debug("the number is : {}",number);
    }

    public static void testInterrupt() throws InterruptedException {

        Thread thread = new Thread(()->{
            try {
                // 休眠 10 s
                TimeUnit.SECONDS.sleep(10);
                log.debug("休眠结束..............");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1");
        thread.start();

        log.debug("线程 t1 的 interrupt 标志是 ： {}",thread.isInterrupted());

        TimeUnit.SECONDS.sleep(1);
        // 打断 thread
        thread.interrupt();
        log.debug("线程 t1 的 interrupt 标志是 ： {}",thread.isInterrupted());
    }
}
